package com.yunkeji.config.keymonitor.task;

import com.google.common.cache.LoadingCache;
import com.yunkeji.common.constant.CacheConstants;
import com.yunkeji.config.init.CacheConfig;
import com.yunkeji.config.keymonitor.config.RedisListenerFactory;
import com.yunkeji.config.keymonitor.config.RedisListenerStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 老张
 * @version V1.0
 * @description 缓存更新-当后台缓存有更新的时候,通知其他机器更新内置缓存, 百度 分布式缓存同步
 * @date 2021/11/15 14:10
 * @update 2021/11/15 14:10
 */
@Component
@Slf4j
public class CacheUpdate implements InitializingBean, RedisListenerStrategy {
    @Resource(name = "sensitiveWordCheck")
    LoadingCache sensitiveWordCheck;

    @Autowired CacheConfig cacheConfig;

    @Override
    public void afterPropertiesSet() throws Exception {
        RedisListenerFactory.put(CacheConstants.CACHE_UPDATE_SENSITIVE, this);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execute(String keyInfo) {
        log.info("分布式缓存同步化违禁词缓存,KEY[{}]", keyInfo);
        cacheConfig.sensitiveWordInit();
        sensitiveWordCheck.refresh(CacheConstants.SENSITIVE_WORD);
    }
}
